import { _decorator, Component, instantiate, Node, Prefab, size, Size } from 'cc';
import { DEBUG } from 'cc/env';
import { UtilCocos } from 'db://cocos-ext/base/UtilCocos';
import WidgetExt, { IAlignParam } from 'db://cocos-ext/base/WidgetExt';
import { games } from 'yycore';

import { CoinNode } from './CoinNode';

const { ccclass, property } = _decorator;


@ccclass('SlotTopBar')
export class SlotTopBar extends Component {

    // 对象模型
    @property([Prefab])
    private readonly modules: Prefab[] = [];


    protected onLoad(): void {
        if (!this.modules) return;

        this.modules.forEach(pb => {
            if (!pb) return;
            switch (pb.name) {
                case "CoinNode":
                    this.initCoinNode(pb, games.base.MoneyID, { left: 80, vCenter: 5 }, size(234, 41));
                    this.initCoinNode(pb, games.base.DiamondID, { left: 355, vCenter: 5 }, size(180, 41));
                    break;
                case "UserExpLvInfo":
                    this.addNode(pb, { vCenter: 5, right: 193 });
                    break;
                case "BtnHome":
                    this.addNode(pb, { vCenter: 5, left: 17 });
                    break;
                case "BtnSetting":
                    this.addNode(pb, { vCenter: 5, right: 17 });
                    break;
                case "BtnHelp":
                    this.addNode(pb, { vCenter: 5, right: 87 });
                    break;
            }
        });
    }

    // 初始化左侧货币节点
    private initCoinNode(pb: Prefab, id: number, p: IAlignParam, s: Size) {
        const node: Node = instantiate(pb);
        if (!node) return;
        const comp: CoinNode = node.getComponent(CoinNode);
        if (!comp) {
            DEBUG && console.log("[SlotTopBar] initCoinNode 对象组件失败！prefab name:", pb.name);
            return;
        }
        UtilCocos.setNodeSize(node, s);
        comp.setMoneyType(id);
        this.node.addChild(node);
        WidgetExt.reset(node, p);
    }

    // 初始化右侧经验节点
    private addNode(pb: Prefab, p: IAlignParam) {
        const node: Node = instantiate(pb);
        if (!node) return;
        this.node.addChild(node);
        WidgetExt.reset(node, p);
    }

}

